﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XStudio.XCRF.Data.Entities;
using XStudio.XCRF.Data.Business.Permissions;
using XStudio.XCRF.Data.Business;
using Telerik.Web.Mvc;
using System.Data.Entity;

namespace XStudio.XCRF.Web.Controllers
{
    /// <summary>
    /// 患者控制器
    /// </summary>
    public partial class PatientController : BaseController
    {
        /// <summary>
        /// 处理添加患者的页面请求
        /// </summary>
        /// <returns></returns>
        [DefaultPermission(PermissionType.Allow)]
        public ActionResult PatientAdd()
        {
            Patient patient = new Patient();
            return View(patient);
        }

        /// <summary>
        /// 处理添加患者的POST请求
        /// </summary>
        /// <param name="patient">欲添加的患者</param>
        /// <returns></returns>
        [DefaultPermission(PermissionType.Allow)]
        [HttpPost]
        public ActionResult PatientAdd(Patient patient)
        {
            try
            {
                DataContext.Patients.Add(patient);
                DataContext.SaveChanges();
                //return Json(patient);
                return Json(new { ID = patient.ID });
            }
            catch
            {
                return Json(null);
            }
        }

        /// <summary>
        /// 处理查看患者详情的页面请求
        /// </summary>
        /// <param name="patientID">患者ID</param>
        /// <returns></returns>
        [DefaultPermission(PermissionType.Allow)]
        public ActionResult PatientDetail(Guid patientID)
        {
            Patient patient = DataContext.Patients.Find(patientID);
            return View(patient);
        }

        /// <summary>
        /// 处理编辑患者的页面请求
        /// </summary>
        /// <param name="patientID">患者ID</param>
        /// <returns></returns>
        [DefaultPermission(PermissionType.Allow)]
        public ActionResult PatientEdit(Guid patientID)
        {
            Patient patient = DataContext.Patients.Find(patientID);
            return View(patient);
        }

        /// <summary>
        /// 处理编辑患者的POST请求
        /// </summary>
        /// <param name="patient">欲编辑的患者</param>
        /// <returns></returns>
        [DefaultPermission(PermissionType.Allow)]
        [HttpPost]
        public ActionResult PatientEdit(Patient patient)
        {
            bool bResult;
            try
            {
                Patient oPatient = DataContext.Patients.Find(patient.ID);
                if (oPatient == null)
                    throw new Exception("患者不存在");
                oPatient.Name = patient.Name;
                oPatient.Gender = patient.Gender;
                oPatient.Birthday = patient.Birthday;
                oPatient.TreatDepart = patient.TreatDepart;
                oPatient.Nationality = oPatient.Nationality;
                oPatient.Address = patient.Address;
                oPatient.Phone = patient.Phone;
                oPatient.Job = patient.Job;
                oPatient.JobKind = patient.JobKind;
                oPatient.BirthPlace = patient.BirthPlace;
                oPatient.Marriage = patient.Marriage;
                oPatient.SocialID = patient.SocialID;
                DataContext.SaveChanges();
                bResult = true;
            }
            catch { bResult = false; }
            var result = new { success = bResult };
            return Json(result);
        }

        [DefaultPermission(PermissionType.Allow)]
        public ActionResult PatientListSelf()
        {
            ViewBag.IDCard = Request.Form["IDCard"];
            ViewBag.Name = Request.Form["Name"];
            ViewBag.Phone = Request.Form["Phone"];
            ViewBag.SocialID = Request.Form["SocialID"];
            return View();
        }

        [DefaultPermission(PermissionType.Allow)]
        [GridAction]
        public ActionResult _PatientListSelf()
        {
            IEnumerable<Patient> patients = DataContext.CRFs
                .Find(crf => crf.HospitalID == HospitalID).Select(crf => crf.Patient).ToList().Distinct();
            List<Patient> dtos = new List<Patient>();
            foreach (Patient patient in patients.ToList())
            {
                Patient dto = new Patient();
                dto.ID = patient.ID;
                dto.IDCard = patient.IDCard;
                dto.Name = patient.Name;
                dto.Gender = patient.Gender;
                dto.Nationality = patient.Nationality;
                dto.Birthday = patient.Birthday;
                dto.Phone = patient.Phone;
                dto.Job = patient.Job;
                dto.JobKind = patient.JobKind;
                dto.Address = patient.Address;
                dto.SocialID = patient.SocialID;
                dtos.Add(dto);
            }
            return Json(new GridModel<Patient>
            {
                Data = dtos
            });
        }

        //TODO: 患者控制器待完成
    }
}
